home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 May / PCpro_2006_05.ISO / files / mobile / fma-2.0-stable-setup.exe / {app} / sframework / plugins / iTunes.vbs < prev    next >
Encoding:
Text File  |  2004-12-10  |  11.6 KB  |  464 lines

  1. 'FMA Script Framework Plugin
  2. 'iTunes
  3. 'Lets you control Apple iTunes
  4.  
  5. 'TODO:
  6. '-Testing
  7.  
  8. const ITUNES_SEARCHTYPE_PL = 0
  9. const ITUNES_SEARCHTYPE_LIB = 1
  10.  
  11. Class iTunes
  12.  
  13. Private m_Self
  14. Private mainMenu
  15. Private BrowsePlaylistMenu
  16. Private SearchMenu
  17.  
  18.  
  19.  
  20. 'Some info about the plugin
  21. Public Property Get SHOWABLE 'Do I have a menu?
  22.  SHOWABLE = True
  23. End Property
  24.  
  25. Public Property Get TITLE 'What's my name?
  26.  TITLE = "iTunes"
  27. End Property
  28.  
  29. Public Property Get DESCRIPTION 'What's my purpose?
  30.  DESCRIPTION = "Lets you control Apple iTunes"
  31. End Property
  32.  
  33. Public Property Get AUTHOR 'Who created me?
  34.  AUTHOR = "ElmarKirchner & Stumo (inspired by streawkceur (inspired by daveo))"
  35. End Property
  36.  
  37. Public Property Get URL 'Were can I be found? Where can you get more information?
  38.  URL = "http://fma.xinium.com/"
  39. End Property
  40.  
  41. 'Who am I?
  42. Public Property Let Self (s)
  43.  m_Self = s
  44.  ' Some init stuff here:
  45.  If IsEmpty(Settings(Me, "Title")) or Settings(Me, "Title") = "" Then Settings(Me, "Title") = "iTunes"
  46.  If IsEmpty(Settings(Me, "Exe")) or Settings(Me, "Exe") = "" Then Settings(Me, "Exe") = "C:\Program Files\iTunes\iTunes.exe"
  47.  Set mainMenu = New ManagedMenu
  48.  mainMenu.Title = TITLE
  49.  
  50.  Set BrowsePlaylistMenu = New ManagedMenu
  51.  Set SearchMenu = New ManagedMenu
  52. End Property
  53.  
  54. Public Property Get Self
  55.  Self = m_Self
  56. End Property
  57.  
  58. 'Display me. Eventually put a menu on the screen
  59. Sub Show()
  60.  Dim iTunesApp
  61.  Set iTunesApp = CreateObject("iTunes.Application")
  62.  
  63.  '--> Init Menu
  64.  Dim llist
  65.  Set llist = New LinkedList
  66.  Dim bi
  67.  bi = llist.BackInserter
  68.  
  69.  Dim iTunesVersion
  70.  iTunesVersion = iTunesApp.Version
  71.  Debug.InfoMsg "iTunes Version " & iTunesVersion
  72.  
  73.  'If ObjectExist( iTunesApp.CurrentTrack ) Then
  74.  'If Exist(iTunesApp.CurrentTrack) Then
  75.  Debug.InfoMsg "Check if Object does exist!"
  76.  'End If
  77.  
  78.  if iTunesApp.PlayerState = 0 Then
  79.   bi.Item = Array("Play", Self & ".PlayPause")
  80.  Else
  81.   Dim TrackName, TrackArtist
  82.   TrackArtist = iTunesApp.CurrentTrack.Artist
  83.   TrackName = iTunesApp.CurrentTrack.Name
  84.   bi.Item = Array(TrackArtist & " - " & TrackName, Self & ".TrackInfo")
  85.  
  86.   bi.Item = Array("Pause", Self & ".PlayPause")
  87.   bi.Item = Array("Stop", Self & ".Stopp")
  88.  End If
  89.  
  90.  bi.Item = Array("Playlist", Self & ".BrowsePlaylist")
  91.  bi.Item = Array("Prev Track", Self & ".PrevTrack")
  92.  bi.Item = Array("Next Track", Self & ".NextTrack")
  93.  bi.Item = Array("Search Current Playlist", Self & ".SearchPl")
  94.  bi.Item = Array("Search Library", Self & ".SearchLib")
  95.  bi.Item = Array("Close", Self & ".Close")
  96.  
  97.  mainMenu.SetList llist
  98.  mainMenu.ShowMenu
  99. End Sub
  100.  
  101. Sub TrackInfo
  102.  Dim iTunesApp, TrackInfo
  103.  Set iTunesApp = CreateObject("iTunes.Application")
  104.  
  105.  Dim Name, Artist, Album, Length
  106.  Artist = iTunesApp.CurrentTrack.Artist
  107.  Album = iTunesApp.CurrentTrack.Album
  108.  Name = iTunesApp.CurrentTrack.Name
  109.  Length = iTunesApp.CurrentTrack.Time
  110.  
  111.  TrackInfo = Artist & " - " & Album & " - " & Name & " - " & Length
  112.  
  113.  EmptyMenu.ShowMenu
  114.  am.DlgInformation "iTunes", TrackInfo
  115. End Sub
  116.  
  117. Sub BrowsePlaylist
  118.  Dim iTunesApp
  119.  Set iTunesApp = CreateObject("iTunes.Application")
  120.  
  121.  Dim Tracks, PlaylistName
  122.  Set Tracks = iTunesApp.CurrentPlaylist.Tracks
  123.  PlaylistName = iTunesApp.CurrentPlaylist.Name
  124.  
  125.  Dim plist
  126.  Set plist = New LinkedList
  127.  Dim bi
  128.  bi = plist.BackInserter
  129.  
  130.  For cnt = 1 To Tracks.Count
  131.   bi.Item = Array( Tracks.Item(cnt).Artist & " - " & Tracks.Item(cnt).Name, Self & ".PlaySongFromPlaylist " & cnt)
  132.  Next
  133.  
  134.  BrowsePlaylistMenu.SetList plist
  135.  BrowsePlaylistMenu.Title = PlaylistName
  136.  BrowsePlaylistMenu.ShowMenu
  137. End Sub
  138.  
  139. Sub PlaySongFromPlaylist( i )
  140.  Dim iTunesApp
  141.  Set iTunesApp = CreateObject("iTunes.Application")
  142.  
  143.  iTunesApp.CurrentPlaylist.Tracks.Item(i).Play
  144.  BrowsePlaylist
  145. End Sub
  146.  
  147. Sub PlayPause
  148.  Dim iTunesApp
  149.  Set iTunesApp = CreateObject("iTunes.Application")
  150.  
  151.  iTunesApp.PlayPause
  152.  am.Update
  153.  Show
  154. End Sub
  155.  
  156. Sub Play
  157.  Dim iTunesApp
  158.  Set iTunesApp = CreateObject("iTunes.Application")
  159.  
  160.  iTunesApp.Play
  161.  am.Update
  162.  Show
  163. End Sub
  164.  
  165. Sub ResumeTrack
  166.  Dim iTunesApp
  167.  Set iTunesApp = CreateObject("iTunes.Application")
  168.  
  169.  iTunesApp.Resume
  170.  am.Update
  171.  Show
  172. End Sub
  173.  
  174. Sub Stopp
  175.  Dim iTunesApp
  176.  Set iTunesApp = CreateObject("iTunes.Application")
  177.  
  178.  iTunesApp.Stop
  179.  am.Update
  180.  Show
  181. End Sub
  182.  
  183. Sub PrevTrack
  184.  Dim iTunesApp
  185.  Set iTunesApp = CreateObject("iTunes.Application")
  186.  
  187.  iTunesApp.PreviousTrack
  188.  am.Update
  189.  Show
  190. End Sub
  191.  
  192. Sub NextTrack
  193.  Dim iTunesApp
  194.  Set iTunesApp = CreateObject("iTunes.Application")
  195.  
  196.  iTunesApp.NextTrack
  197.  am.Update
  198.  Show
  199. End Sub
  200.  
  201. Sub FastForward
  202.  Dim iTunesApp
  203.  Set iTunesApp = CreateObject("iTunes.Application")
  204.  
  205.  iTunesApp.FastForward
  206.  am.Update
  207.  Show
  208.  End Sub
  209.  
  210. Sub Rewind
  211.  Dim iTunesApp
  212.  Set iTunesApp = CreateObject("iTunes.Application")
  213.  
  214.  iTunesApp.Rewind
  215.  am.Update
  216.  Show
  217. End Sub
  218.  
  219. Sub Close
  220.  Dim iTunesApp
  221.  Set iTunesApp = CreateObject("iTunes.Application")
  222.  
  223.  iTunesApp.Quit
  224.  am.Update
  225. End Sub
  226.  
  227. Sub SearchPl
  228.  Debug.DebugMsg "SearchPl"
  229.  EmptyMenu.ShowMenu
  230.  Dim iTunesApp
  231.  Set iTunesApp = CreateObject("iTunes.Application")
  232.  Dim TitleString
  233.  TitleString = "Search " & iTunesApp.CurrentPlaylist.Name
  234.  Debug.DebugMsg  "Search Title = " &  TitleString 
  235.  am.DlgInputStr "Search", "Search For:", 16, "", Self & ".SearchPlResult"
  236. End Sub
  237.  
  238. Sub SearchLib
  239.  EmptyMenu.ShowMenu
  240.  am.DlgInputStr "Search Library", "Search For:", 16, "", Self & ".SearchLibResult"
  241. End Sub
  242.  
  243. Sub SearchPlResult (input)
  244.  MenuStack.Top.Quit 'remove empty menu
  245.  ChooseTrack ITUNES_SEARCHTYPE_PL,input 
  246. End Sub
  247.  
  248. Sub SearchLibResult (input)
  249.  MenuStack.Top.Quit 'remove empty menu
  250.  ChooseTrack ITUNES_SEARCHTYPE_LIB,input
  251. End Sub
  252.  
  253. Sub ChooseTrack(searchtype, input)
  254.  EmptyMenu.ShowMenu
  255.  am.DlgFeedback "Working", Self & ".DlgFeedbackResult"
  256.  am.Update
  257.  Dim iTunesApp
  258.  Set iTunesApp = CreateObject("iTunes.Application")
  259.  Dim iTunesPlaylist
  260.  if(searchtype = ITUNES_SEARCHTYPE_PL) then
  261.   Set iTunesPlaylist = iTunesApp.CurrentPlaylist
  262.  elseif(searchtype = ITUNES_SEARCHTYPE_LIB) then
  263.   Set iTunesPlaylist = iTunesApp.LibraryPlaylist
  264.  end if
  265.  Dim Tracks
  266.  Set Tracks = iTunesPlaylist.Search(input, 0) 'all fields
  267.  if (Tracks is nothing) then
  268.   EmptyMenu.ShowMenu
  269.   am.DlgMsgBox "No Matches", 5
  270.   am.Update
  271.  else
  272.   Dim matchList
  273.   if(Tracks.Count>300) then
  274.    am.DlgMsgBox "More than 300 Tracks returned. Results are unsorted"
  275.    am.Update
  276.    Set matchList = new LinkedList
  277.    Dim bi
  278.    Set bi = matchList.BackInserter
  279.    for cnt = 1 to Tracks.Count
  280.     bi.Item = Array (Tracks.Item(cnt).Artist & " - " & Tracks.Item(cnt).Name, Self & ".ChooseTrackResult " & searchtype & ", " & Tracks.Item(cnt).SourceID & ", " & Tracks.Item(cnt).PlaylistID & ", " & Tracks.Item(cnt).TrackID & ", " & Tracks.Item(cnt).TrackDatabaseId)
  281.    next
  282.   else 'This sorts the tracks into alphabetical order 
  283.    'see comments with the class at the end of the file for how it does it
  284.    Dim Tree
  285.    Set Tree = new ITunesAlphabetTree
  286.    Dim Comparator
  287.    Set Comparator = New ItunesLLArrayItem1Comparator
  288.    Tree.SetComparator Comparator
  289.    For cnt = 1 To Tracks.Count 
  290.     Tree.Add Array (Tracks.Item(cnt).Artist & " - " & Tracks.Item(cnt).Name, Self & ".ChooseTrackResult " & searchtype & ", " & Tracks.Item(cnt).SourceID & ", " & Tracks.Item(cnt).PlaylistID & ", " & Tracks.Item(cnt).TrackID & ", " & Tracks.Item(cnt).TrackDatabaseId)
  291.    Next  
  292.    Set matchList = Tree.GetLinkedList
  293.   end if
  294.   SearchMenu.SetList matchList
  295.   SearchMenu.Title = "Search Results"
  296.   MenuStack.Top.Quit 'remove empty menu
  297.   SearchMenu.ShowMenu
  298.  end if
  299. End Sub
  300.  
  301. Sub ChooseTrackResult(searchtype, SourceID, PlaylistID, TrackID, TrackDbId)
  302.  Dim iTunesApp
  303.  Set iTunesApp = CreateObject("iTunes.Application")
  304.  Debug.DebugMsg "Trying now..."
  305.  Dim track
  306.  Set track = iTunesApp.GetITObjectByID(SourceID, PlaylistID, TrackID, TrackDbId)
  307.  Debug.DebugMsg "We have a track object - " 
  308.  Debug.DebugMsg track.Name
  309.  if(searchtype = ITUNES_SEARCHTYPE_PL) then
  310.   track.Play
  311.  elseif (searchtype = ITUNES_SEARCHTYPE_LIB) then
  312.   dim PartyShuffle
  313.   set PartyShuffle = iTunesApp.LibrarySource.Playlists.ItemByName("Party Shuffle")
  314.   dim newTrack
  315.   Set newTrack = PartyShuffle.AddTrack(track)
  316.   if(iTunesApp.PlayerState <> 1) then 
  317.    Debug.DebugMsg "Player state was " & iTunesApp.PlayerState 
  318.    newTrack.Play
  319.   elseif (PartyShuffle.PlaylistID <> iTunesApp.CurrentPlaylist.PlaylistID) then
  320.    Debug.DebugMsg "Current Playlist was "& iTunesApp.CurrentPlaylist.Name
  321.    newTrack.Play
  322.   else
  323.    'This code is designed to shuffle the tracks in the PartyShuffle so that
  324.    'The newly added one is the next played. It does this by moving all tracks
  325.    'in between to the bottom of the playlist. Slow and ugly, but it works.
  326.    dim Tracklist
  327.    Set Tracklist = PartyShuffle.Tracks
  328.    dim count
  329.    count = 2
  330.    dim foundTrack
  331.    foundTrack=false    
  332.    while(count < Tracklist.count)
  333.     Dim thisTrack
  334.     Set thisTrack = Tracklist.ItemByPlayOrder(count)
  335.     if(thisTrack.TrackID = newTrack.TrackId) then
  336.      ' We just added this track, break out of the loop
  337.      count = TrackList.count
  338.     elseif(thisTrack.TrackID = iTunesApp.CurrentTrack.TrackID) then
  339.      ' This is the currently playing track, shift stuff after here"
  340.      foundTrack = true
  341.      count = count + 1
  342.     elseif(foundTrack) then
  343.      if(thisTrack.TrackID = iTunesApp.CurrentTrack.TrackID) then
  344.       'Play shifted while we were fiddling
  345.       count = count+1
  346.      else
  347.       'Adding to the end
  348.       PartyShuffle.AddTrack thisTrack
  349.       'Removing from beginning
  350.       thisTrack.Delete
  351.      end if
  352.     else
  353.      count = count+1
  354.     end if    
  355.    wend
  356.   end if
  357.  end if
  358.  MenuStack.Top.Quit
  359. End Sub
  360.  
  361. End Class
  362.  
  363. 'This stupidly named class does a lower case compare on the first array 
  364. 'element of the two supplied arrays
  365.  
  366. Class ItunesLLArrayItem1Comparator
  367. Public Function Compare( arrA, arrB )
  368.   'compare case insensitive
  369.   dim a,b
  370.   a=Lcase(arrA(0))
  371.   b=Lcase(arrB(0))
  372. '   Debug.DebugMsg "Comparing " & a & " & " & b
  373.   If a = b Then
  374.    Compare = 0
  375.   ElseIf a < b Then
  376.    Compare = -1
  377.   Else
  378.    Compare = 1
  379.   End If
  380. End Function
  381. End Class
  382.  
  383.  
  384. 'This is a tree designed to sort the items placed into it according to the comparator, so
  385. 'it can then be output as a linked list later
  386. 'This may be faster than a quicksort. I'm not sure, I haven't tested it. It'll do best when stuff
  387. 'comes at it in a random order.
  388.  
  389. Class iTunesAlphabetTree
  390. private topNode
  391. private comp
  392. Sub SetComparator(Comparator)
  393.  set comp = Comparator
  394. End Sub
  395.  
  396. Sub Add(Item)
  397.  if(isObject(topNode)) then
  398.   topNode.Add Item,comp
  399.  else
  400.   Set topNode = new iTunesAlphabetNode
  401.   topNode.SetItem Item
  402.  end if
  403. End Sub
  404.  
  405. Function GetLinkedList
  406.  dim ll
  407.  Set ll = new LinkedList
  408.  dim bi
  409.  Set bi = ll.BackInserter
  410.  if(isObject(topNode)) then
  411.   topNode.Iterate bi
  412.  end if
  413.  Set GetLinkedList = ll
  414. End Function
  415. End Class
  416.  
  417. ' The node for the above tree
  418.  
  419. Class iTunesAlphabetNode
  420. private my_item
  421. private prevnode
  422. private nextnode
  423.  
  424. sub SetItem (Item)
  425.  if(isObject (Item)) then
  426.   Set my_item = Item
  427.  else
  428.   my_item = Item
  429.  end if
  430. end sub
  431.  
  432. Sub Add(Item, comp)
  433.  if(comp.Compare(Item, my_item) <0) then
  434.   if(isObject (prevnode)) then
  435.    prevnode.Add Item, comp
  436.   else
  437.    Set prevnode = new iTunesAlphabetNode
  438.    prevnode.SetItem Item
  439.   end if
  440.  else
  441.   if(isObject (nextnode)) then
  442.    nextnode.Add Item, comp
  443.   else
  444.    Set nextnode = new iTunesAlphabetNode
  445.    nextnode.SetItem Item
  446.   end if
  447.  end if
  448. end sub
  449.  
  450. Sub Iterate(bi)
  451.  if(isObject (prevnode)) then
  452.   prevnode.Iterate bi 
  453.  end if
  454.  if(isObject(my_item)) then
  455.   Set bi.Item = my_item
  456.  else
  457.   bi.Item = my_item
  458.  end if
  459.  if(isObject (nextnode)) then
  460.   nextnode.Iterate bi
  461.  end if   
  462. end sub
  463. End Class
  464.